<template>
  <div>
    <el-form :inline="true" :model="filters" :rules="searchFormRules" ref="filters"  class="demo-form-inline" style="margin-top:20px;">
      <el-form-item label="字典类型" style="margin-right:40px;" prop="value">
        <el-input v-model="filters.value" placeholder="请输入..."></el-input>
      </el-form-item>
      <el-form-item label="类型代码" style="margin-right:40px;" prop="code">
        <el-input v-model="filters.code" placeholder="请输入..."></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch()">查询</el-button>
        <el-button type="danger" @click="resetForm('filters')">重置</el-button>
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table :data="getAllDict" :border="true" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="expand" align="center">
        <!--嵌套-->
        <template slot-scope="props">
          <el-table :data="props.row.dictSubList" :show-header="false" :border="true" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%">
            <el-table-column type="index" align="center">
            </el-table-column>
            <el-table-column prop="value" label="字典类型" align="center">
            </el-table-column>
            <el-table-column prop="code" label="类型代码" align="center">
            </el-table-column>
            <el-table-column prop="name" label="备注" align="center">
            </el-table-column>
            <el-table-column prop="sort" label="排序" align="center">
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">

                <el-button type="danger" size="mini" @click="handleDel(scope.$index, scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column prop="value" label="字典类型" align="center">
      </el-table-column>
      <el-table-column prop="code" label="类型代码" align="center">
      </el-table-column>
      <el-table-column prop="name" label="备注" align="center">
      </el-table-column>
      <el-table-column prop="sort" label="排序" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>


    </el-table>





    <!-- 分页 -->
    <div style="float:right;margin-top:20px;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[10, 15, 20, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount">
      </el-pagination>
    </div>
    <!--新增界面-->
    <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item label="字典类型" prop="code">
          <el-input v-model="addForm.code" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型代码" prop="value">
          <el-input v-model="addForm.value" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="父级字典">
          <el-select @change="getNewCode" v-model="addValue" placeholder="请选择">
            <el-option
              v-for="item in getAddMenuData"
              :key="item.value"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="name">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="addForm.sort" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click="addSubmit('addForm')" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
    <!--编辑界面-->
    <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="字典类型" prop="value">
          <el-input v-model="editForm.value" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型代码" prop="code">
          <el-input v-model="editForm.code" auto-complete="off" :disabled="true"></el-input>
        </el-form-item>
        <!--  <el-form-item label="父级字典">
		   <el-select @change="getNewCode" v-model="value1" placeholder="请选择">
			 <el-option
			   v-for="item in getAddMenuData"
			   :key="item.value"
			   :label="item.value"
			   :value="item.code">
			 </el-option>
		   </el-select>
		 </el-form-item>-->
        <el-form-item label="备注" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="editForm.sort" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {fetch, post} from '@/utils/http'
  export default {
    data() {
      let checkSort = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入排序'));
        } else {
          callback()
        }
      };
      let checkCode = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入字典类型'));
        } else {
          callback()
        }
      }
      return {
        getAllDict: [],
        dictSubList: [],
        totalCount: 0,
        pageIndex: 1,
        pageSize: 10,
        listLoading: false,
        sels: [],//列表选中列
        addValue:'',
        editValue: '',
        value1: '',
        getAddMenuData: [],
        filters:{
          value:"",
          code: ""
        },
        searchFormRules:{
          value: [
            { required: true, message: '请输入字典类型', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '请输入类型代码', trigger: 'blur' }
          ]
        },
        //编辑界面是否显示
        editFormVisible: false,
        editLoading: false,
        editFormRules: {
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ]
        },
        //编辑界面数据
        editForm: {
          name: '',
          code: '',
          sort: '',
          status: '',
          value: ''
        },
        newParsentCode: '',
        //新增界面是否显示
        addFormVisible: false,
        addLoading: false,
        addFormRules: {
          sort: [
            { validator: checkSort, trigger: 'blur' }
          ],
          code: [
            { validator: checkCode, trigger: 'blur' }
          ]
        },
        //新增界面数据
        addForm: {
          code: '',
          value: '',
          parentCode: '',
          name: '',
          sort: ''
        }
      }
    },
    methods: {
      checkDetail(row, expandedRows) {
        console.log(1);
        console.log(row);
      },
      //显示编辑界面
      handleEdit: function (index, row) {
        this.editFormVisible = true;
        this.getAddMenu();
        this.editForm = Object.assign({}, row);
        this.editValue = this.editForm.name;
      },
      editSubmit() {
        this.$refs.editForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true;
              let para = Object.assign({}, this.editForm);
              console.log(para);
              post('/api/manager/v1/dict/updateDict',para).then((response) => {
                this.editLoading = false;
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                this.$refs['editForm'].resetFields();
                this.editFormVisible = false;
                this.getDict();
              });
            });
          }
        });
      },
      //删除
      handleDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          post('/api/manager/v1/dict/updateDict',{id: row.id,status: 1}).then((response) => {
            console.log(response);
            this.listLoading = false;
            if(response.data == 2){
              this.$message({
                message: '请先删除子集',
                type: 'error'
              });
            }else if(response.data == 1){

              this.$message({
                message: '删除成功',
                type: 'success'
              });
            }


            this.getDict();
          });
        }).catch(function(err) {
          console.log(err)
        });
      },
      //获取当前下拉框选中值的value代码值
      getNewCode(value) {
        this.newParsentCode = value;
        console.log(this.newParsentCode);
      },
      // 添加
      addSubmit: function (addForm) {
        this.$refs.addForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              let _this = this;
              this.addLoading = true;
              let para = Object.assign({}, this.addForm);
              //获取当前选中的字典类型的value,如果不选parentCode==0
              if(!this.newParsentCode){
                para.parentCode = 0;
              }else{
                para.parentCode = this.newParsentCode;
              }
              post('/api/manager/v1/dict/addDict',para).then((response) => {
                this.addLoading = false;
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                this.addFormVisible = false;
                this.getDict();
              });
            })
          }
        });
      },

      //显示新增界面
      handleAdd: function () {
        this.addFormVisible = true;
        this.getAddMenu();
        this.addForm = {
          code: '',
          value: '',
          parentCode: '',
          name: '',
          sort: ''
        };
      },
      //添加时获取字典菜单列表
      getAddMenu() {
        this.listLoading = true;
        let _this = this;
        let para = {parentCode: '0',status: 0};
        post('/api/manager/v1/dict/findDict', para).then((response) => {
          this.getAddMenuData = response.data.data;
          let arr = {code:'0',value:'父级'}
          _this.getAddMenuData.push(arr)
          console.log(_this.getAddMenuData)
          this.listLoading = false;
        }).catch(function(err) {
          console.log(err)
        });
      },

      handleSizeChange(val) {
        this.pageSize = val
        this.getDict();
      },
      handleCurrentChange(val) {
        this.pageIndex = val;
        this.getDict();
      },
      // 重置
      resetForm(editForm) {
        this.$refs[editForm].resetFields();
      },
      selsChange: function (sels) {
        this.sels = sels;
      },
      //搜索
      onSearch() {
        let para = {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize
        };
        if(!this.filters.code.replace(/\s/g, "") == '') {
          para.code = this.filters.code.replace(/\s/g, "")
        }
        if(!this.filters.value.replace(/\s/g, "") == '') {
          para.value = this.filters.value.replace(/\s/g, "")
        }
        this.listLoading = true;
        let _this = this;
        post('/api/manager/v1/dict/queryPageByParent',para).then((response) => {
          this.getAllDict = response.data.data;
          this.total = response.data.totalCount;
          //this.size = response.data.pageSize;
          console.log(this.getAllDict)
          this.listLoading = false;
        }).catch(function(err) {
          console.log(err)
        });
      },
      //获取列表
      getDict() {
        let para = {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize
        }
        this.listLoading = true;
        let _this = this;
        post('/api/manager/v1/dict/queryPageByParent',para).then((response) => {
          this.getAllDict = response.data.data;
          this.totalCount = response.data.totalCount
          this.listLoading = false;
        }).catch(function(err) {
          console.log(err)
        });
      },
    },
    mounted() {
      this.getDict();

    }
  }
</script>
<style>
  .demo-form-inline {
    height: 60px;
    padding-top: 20px;
    margin-bottom: 60px;
    background-color: #66BFBF;
    border-radius: 6px;
  }
</style>
